package main

import "fmt"

type User struct {
	Id   int32
	Name string `json:"name"`
}

type Account struct {
	User
	password string
}

type Contact struct {
	*User
	Remark string
}

func Struct() {
	var u1 User = User{
		Name: "张三",
	}
	u1.Id = 1000
	//var u2 *User=new(User)
	var u2 *User = &User{
		Name: "李四",
	}
	u2.Id = 1001 //(*u2).Id=1001
	fmt.Println("u2=", u2)

	var account1 = Account{
		User:     u1,
		password: "666",
	}

	fmt.Println("account1=", account1)

	var contact1 *Contact = &Contact{
		User:   u2,
		Remark: "zhangsan",
	}
	contact1.Name = "李四2" //contact1.User.Name = "张三contact"
	fmt.Println("contact1=", contact1)
	fmt.Println("contact1.User=", contact1.User)
}

//自定义的数据类型，属于不同类型，混用需要类型转换
func TypeDefinitionAndTypealias() {
	fmt.Println("4.4.1 自定义数据")
	type mesType uint16
	var u1000 uint16 = 1000
	var textMes mesType = mesType(u1000)
	fmt.Printf("textMes=%v,type of textMes=%T\n", textMes, textMes)

	type myUnit16 = uint16
	var myu16 myUnit16 = u1000
	fmt.Printf("myu16=%v,type of myu16=%T\n", myu16, myu16)
}

//map引用类型，默认是nil
func Map() {
	var m1 map[string]string
	fmt.Println("does m1==nill?", m1 == nil)

	m1 = make(map[string]string, 2) //初始size默认为1
	m1["早上"] = "鸡蛋"
	m1["中午"] = "米饭"
	m1["晚上"] = "面条"
	fmt.Println("m1[\"早上\"]", m1["早上"])

	m2 := map[string]string{
		"晚上": "米粉",
		"凌晨": "烧烤",
	}
	fmt.Println("m2", m2)

	//通过双赋值检测某个key是否存在
	v, ok := m2["凌晨"]
	if ok {
		fmt.Println("v=", v)
	} else {
		fmt.Println("key不存在")
	}

	delete(m2, "晚上")
	fmt.Println("m2", m2)

	for i, v := range m1 {
		fmt.Printf("m1[%v]=%v\n", i, v)
	}
}

func SelectKey(text ...string) (key int) {
	for i, v := range text {
		fmt.Printf("%v: %v\n", i, v)
	}
	fmt.Println("请选择数字")
	fmt.Scanln(&key)
	return
}

func Array() {
	//4.1数组
	var a = [...]int{
		1,
		2,
		3,
	}
	a[0] = 123
	for i, v := range a {
		fmt.Printf("a[%v]=%v\n", i, v)
	}

	var twoDimensionalArray = [3][4]int{
		{1, 2, 3},
		{1, 2, 3},
		{1, 2, 3},
	}

	for i2, v2 := range twoDimensionalArray {
		for i3, v3 := range v2 {
			fmt.Printf("twoDimensionalArray[%v][%v]=%v\t", i2, i3, v3)
		}
		fmt.Printf("\n")
	}

	//4.2切片
	array1 := [5]int{1, 2, 3, 4, 5}
	var s1 []int = array1[1:4]
	fmt.Println("s1=", s1)
	fmt.Println("array1=", array1)

	s2 := array1[1:]
	fmt.Println("s2=", s2)
	fmt.Println("array1=", array1)

	var s3 []int
	fmt.Println("does s3==nill?", s3 == nil)
	fmt.Println("s3=", s3)

	s3 = make([]int, 3, 5) //分配内存地址,创建了长度为3的数组，最长可以装5个
	fmt.Println("s3=", s3)
	fmt.Printf("len(s3)=%v,cap(s3)=%v\n", len(s3), cap(s3))

	s4 := []int{1, 2, 3}
	fmt.Println("s4=", s4)

	s2 = append(s1, 888, 999)
	fmt.Println("append s2=", s2)
	fmt.Println("array1=", array1)

	s2 = append(s1, s2...)
	fmt.Println("append s2=", s2)

	s6 := make([]int, 2)
	fmt.Println("s6=", s6)
	copy(s6, s2) //容量能接受多少，就接收多少
	fmt.Println("s6=", s6)

	str := "hello 世界"
	fmt.Printf("%v\n[]byte(str)=%s\n", []byte(str), []byte(str))

	posbyte := []byte{
		13, 132, 235, 231, 194, 21, 142, 28, 30, 193, 29, 84, 143, 145, 66, 37, 0, 0, 135, 67,
	}
	//str1 := string(posbyte[:])
	str1 := fmt.Sprintf("%s", posbyte)
	fmt.Printf("posbyte=%v\n", str1)

	for i, v := range str {
		fmt.Printf("str[%d]=%c\n", i, v) //字符串底层是byte切片
	}
}

func main() {
	Array()
	//key := SelectKey("注册", "登录", "退出")
	//fmt.Println("接收到key=", key)

	Map()
	TypeDefinitionAndTypealias()
	Struct()
}
